/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package di.persistence.entities;

import di.persistence.entities.interfaces.ILevelable;
import di.persistence.entities.interfaces.IEntity;
import di.persistence.entities.interfaces.ILabeled;
import di.persistence.entities.interfaces.ITree;
import di.persistence.object.UserTypeBO;
import di.server.context.AppContext;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

/**
 *
 * @author Kilian Ito
 */
@Entity
@Table(uniqueConstraints={
    @UniqueConstraint(columnNames={"label"})
})
public class UserType implements IEntity, ILabeled {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;
    protected String label;
    
    @Fetch(FetchMode.SELECT)
    @OneToMany(cascade={CascadeType.REFRESH, CascadeType.MERGE}, fetch=FetchType.LAZY)
    protected List<UserType> children;

    public UserType() {
        
    }
    
    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String getLabel() {
        return label;
    }

    @Override
    public void setLabel(String label) {
        this.label = label;
    }

    public List<UserType> getChildren() {
    if (children == null || children.isEmpty())
        children = new ArrayList<>();
    children=AppContext.getBean(UserTypeBO.class).getChildren(this);
    if (children == null)
        children = new ArrayList<>();
    return children;
    }
    
    
    
    
    

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof UserType)) {
            return false;
        }
        UserType other = (UserType) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return getLabel();
    }
    
}
